package com.atguigu.yygh.cmn.controller;

import com.atguigu.yygh.cmn.service.IDictService;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author kabuda
 * @create 2023-02-24 8:44
 */
@Api(tags = "字典控制层")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Resource
    private IDictService dictService;

    @ApiOperation("导出字典数据")
    @GetMapping("/exportData")
    public void exportExcel(HttpServletResponse response){
        dictService.exportExcel(response);
    }

    @ApiOperation("导入字典数据")
    @PostMapping("/importData")
    public void importExcel(MultipartFile file){
        dictService.importExcel(file);
    }

    @ApiOperation("根据id查询字典数据")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable("id") Long id){
       List<Dict> list= dictService.findChildData(id);
       return  Result.ok(list);
    }

    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode, @PathVariable("value") String value){
       String name= dictService.getName(dictCode,value);
       return name;
    }
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable("value") String value){
       String name= dictService.getName("",value);
       return name;
    }

    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> dicts=dictService.findByDictCode(dictCode);
        return Result.ok(dicts);
    }

}
